package com.qinglei.recoup.longconn.dispatch.service.impl;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.qinglei.recoup.common.utils.JsonUtils;
import com.qinglei.recoup.longconn.dispatch.service.DispatchService;
import com.qinglei.recoup.longconn.dispatch.util.DispatchProperties;
import com.qinglei.recoup.longconn.dispatch.util.RadarJsonUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.File;
import java.io.FileWriter;
import java.time.LocalDate;
import java.util.List;

@Service
@Slf4j
public  class DispatchServiceImpl  implements DispatchService , SchedulingConfigurer {


    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DispatchProperties dispatchProperties;

    @Value("${dispatch.fpathSleepAnalysis:}")
    private String fpathSleepAnalysis;

    ////睡眠报告总
    @Override
    public Runnable DispatchJob() {

        return new Runnable() {
            @SneakyThrows
            @Override
            public void run() {
                log.info("----------开始获取文件--------------");
               /* RadarJsonUtil.getAllTadayJson().forEach(f -> {
                    log.info("----------文件名称--------------"+f);
                    //String f = "D:/C45BBE5257A3_20211117_night.json";
                    ResponseEntity<String> forEntity = restTemplate.getForEntity(fpathSleepAnalysis, String.class, f);
                    String body = forEntity.getBody();
                    log.info(forEntity.getBody() + "+++++++++++++++++++++本地服务器的json数据列表了+++++++++++++++++++++++++++++++++++++++");
                });*/
                //dispatchService.DispatchJob().run();
                List<String> allTadayJson = RadarJsonUtil.getAllTadayJson();
                LocalDate  date =LocalDate.now();
                File file = new File("/opt/app/recoup/filepath/"+date+".json");
                FileWriter writer = new FileWriter(file);
                JSONArray fpath = JSONArray.fromObject(allTadayJson);
                writer.write(fpath.toString());
                writer.close();
                String filepath = file.getAbsolutePath();
                ResponseEntity<String> forEntity = restTemplate.getForEntity(fpathSleepAnalysis,String.class,filepath);
                log.info(file.getAbsolutePath()+"");
            }
        };
    }

    @Override
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {

        log.info("configureTasks in");
        String timeCron = dispatchProperties.getTimeCron();
        System.out.println(timeCron);
        scheduledTaskRegistrar.addCronTask(DispatchJob(), dispatchProperties.getTimeCron());
    }



}
